﻿using AutoMapper;
using Modules.CheckIn.Entitys;
using Modules.Corp;
using Modules.Device;
using Modules.Device.Entitys;
using Modules.Person;
using Modules.Project;
using PmSoft;
using PmSoft.ApplicationContext;
using static Modules.CheckIn.CheckInDetailConstants;
using static Modules.Person.PersonConstants;

namespace ManagementApi.Models.Manage.CheckIn
{
    public class CheckInDetailDto
    {
        /// <summary>
        /// 主键
        ///</summary>
        public string Id { get; set; }

        public int MemberId { get; set; }

        /// <summary>
        /// 人员名称
        ///</summary>
        public string? PersonName { get; set; }

        /// <summary>
        /// 考勤月份
        /// </summary>
        public int Month { get; set; }

        /// <summary>
        /// 考勤状态
        ///</summary>
        public string? StatusName { get; set; }

        /// <summary>
        /// 进出标识
        ///</summary>
        public 进出标识? EntryExitMark { get; set; }

        /// <summary>
        /// 进出标识
        ///</summary>
        public string? EntryExitMarkName { get; set; }

        /// <summary>
        /// 数据来源
        ///</summary>
        public 数据来源? DataSources { get; set; }

        /// <summary>
        /// 数据来源
        ///</summary>
        public string? DataSourcesName { get; set; }

        /// <summary>
        /// 班组名称
        /// </summary>
        public string? GroupName { get; set; }

        /// <summary>
        /// 开始用工日期
        /// </summary>
        public DateTime? StartDate { get; set; }

        /// <summary>
        /// 结束用工日期
        /// </summary>
        public DateTime? EndDate { get; set; }

        /// <summary>
        /// 考勤日期
        ///</summary>
        public DateTime? CheckinDate { get; set; }

        public DateTime? CheckinTime { get; set; }

        /// <summary>
        /// 创建时间
        ///</summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 设备编码
        /// </summary>
        public string? EquipmentCode { get; set; }

        /// <summary>
        /// 所属公司名称
        /// </summary>
        public string CorpName { get; set; } = string.Empty;

        /// <summary>
        /// 所属公司类型
        /// </summary>
        public string CorpTypeName { get; set; } = string.Empty;

        /// <summary>
        /// 工种
        /// </summary>
        public string WorkTypeName { get; set; } = string.Empty;

        /// <summary>
        /// 所属项目名称
        /// </summary>
        public string ProjectName { get; set; } = string.Empty;

        /// <summary>
        /// 是否是管理人员
        /// </summary>
        public bool isManager { get; set; }

        /// <summary>
        /// 设备厂家
        /// </summary>
        public string? DeviceType { get; set; }
    }

    public static partial class CheckInDetailExceptions
    {
        /// <summary>
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static CheckInDetailDto AsCheckInDetailDto(this CheckInDetail entity, IApplicationContext applicationContext, ProjectInfo? project = null)
        {
            CheckInDetailDto dto = new CheckInDetailDto();
            var personService = applicationContext.GetService<PersonService>();
            var checkinDeviceService = applicationContext.GetService<CheckinDeviceService>();
            var projectCorpService = applicationContext.GetService<ProjectCorpService>();
            var corpService = applicationContext.GetService<CorpService>();
            var projectService = applicationContext.GetService<ProjectService>();
            var projectGroupMemberService = applicationContext.GetService<ProjectGroupMemberService>();
            var projectGroupService = applicationContext.GetService<ProjectGroupService>();

            project ??= projectService.GetProjectInfo(entity.ProjectId);
            var checkinDevice = checkinDeviceService.GetProjectCheckinDevice(entity.EquipmentCode ?? "");
            var projectGroup = projectGroupService.GetProjectGroup(entity.GroupId);
            var projectGroupMember = projectGroupMemberService.GetMember(entity.MemberId);
            var projectCorp = projectCorpService.GetProjectCorpByProjectIdAndCorpId(entity.ProjectId, projectGroup?.CorpId ?? 0);
            var corpInfo = corpService.GetCorpInfo(projectGroup?.CorpId ?? 0);
            var personInfo = personService.GetPerson(entity.PersonId);

            dto.Id = entity.Id;
            dto.PersonName = personInfo?.Name;
            dto.StatusName = entity.Status.GetDescription();
            dto.EntryExitMarkName = entity.EntryExitMark.GetDescription();
            dto.DataSourcesName = entity.DataSources.GetDescription();
            dto.CheckinDate = entity.CheckinDate;
            dto.CheckinTime = entity.CheckinTime;
            dto.CreateTime = entity.CreateTime;
            dto.EquipmentCode = entity.EquipmentCode;
            dto.Month = entity.Month;
            dto.MemberId = entity.MemberId;
            dto.WorkTypeName = entity.WorkType?.GetDescription() ?? entity.PostCategory?.GetDescription() ?? string.Empty;
            dto.DeviceType = checkinDevice?.Type.GetDescription() ?? string.Empty;
            dto.GroupName = projectGroup?.Name ?? string.Empty;
            dto.StartDate = projectGroup?.StartDate;
            dto.EndDate = projectGroup?.EndDate;
            dto.isManager = projectGroupMember?.IsManager ?? false;
            if (project?.LumpContractorCorpId == corpInfo?.CorpId)
            {
                dto.CorpTypeName = "总承包";
            }
            else
            {
                dto.CorpTypeName = projectCorp?.Role.GetDescription() ?? string.Empty;
            }
            dto.CorpName = corpInfo?.Name ?? string.Empty;
            dto.ProjectName = project?.Name ?? string.Empty;
            return dto;
        }

        /// <summary>
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static CheckInDetailDto AsAppDto(this CheckInDetail entity)
        {
            CheckInDetailDto dto = new CheckInDetailDto();
            dto.Id = entity.Id;
            dto.StatusName = entity.Status.GetDescription();
            dto.EntryExitMarkName = entity.EntryExitMark.GetDescription();
            dto.DataSourcesName = entity.DataSources.GetDescription();
            dto.CheckinDate = entity.CheckinDate;
            dto.CheckinTime = entity.CheckinTime;
            dto.CreateTime = entity.CreateTime;
            dto.EquipmentCode = entity.EquipmentCode;
            return dto;
        }
    }
}